/**
 * Created by 鹏 on 14-3-10.
 */
var app = angular.module('PowerTeam', ['ui.bootstrap', 'ngSanitize']);

function InstallCtrl($scope, $http) {
    $scope.Config = {MySqlHost: "localhost", MySqlPort: "3306", MySqlDatabase: "powerteam", MySqlUser: "root", MySqlPass: "1111", AdminEmail: "admin@admin.com", AdminPass: "admin"};
    $scope.errorMsg = null;
    $scope.processing = false;
    $scope.runInstall = function () {
        $scope.errorMsg = null;
        $scope.processing = true;
        var btn = $("#btnLogin");
        btn.button('loading');
        $http.post('/RunInstall', $scope.Config).success(function (result) {
            btn.button('reset');
            $scope.processing = false;
            if (result.success) {
                window.location.href = '/Installed';
            }
            else {
                if (result.error == 1) {
                    $scope.errorMsg = "无法连接数据库，请检查数据库配置是否正确。";
                }
            }
        });
    }
};

function SignInCtrl($scope, $http) {
    $scope.isMatch = true;
    $scope.processing = false;
    $scope.signIn = function () {
        $scope.isMatch = true;
        $scope.processing = true;
        var btn = $("#btnSignin");
        btn.button('loading');
        $http.post('/User/Verify', $scope.User).success(function (result) {
            btn.button('reset');
            $scope.processing = false;
            $scope.isMatch = result.isMatch;
            if (result.isMatch) {
                window.location.href = '/Desktop';
            }
        });
    }
};

function SignUpCtrl($scope, $http) {
    $scope.created = true;
    $scope.processing = false;
    $scope.signUp = function () {
        $scope.processing = true;
        $scope.created = true;
        var btn = $("#btnSignup");
        btn.button('loading');
        $http.post('/User/Create', $scope.User).success(function (result) {
            $scope.processing = false;
            $scope.created = result.created;
            btn.button('reset');
            if (result.created) {
                window.location.href = '/Desktop';
            }
        });
    }
};

function NewProjectCtrl($scope, $http) {
    $scope.processing = false;
    $scope.save = function () {
        $scope.processing = true;
        $http.post('/Project/Create', $scope.Project).success(function (data) {
            $scope.processing = false;
            if (data.Result == "OK") {
                window.location.href = '/Desktop';
            }
        });
    }
};

function DashboardCtrl($scope, $http) {
    $('#task-progress').highcharts({
        credits: {
            enabled: false
        },
        chart: {
            type: 'column'
        },
        title: {
            text: '任务完成情况'
        },
        subtitle: {
            text: 'PowerTeam'
        },
        xAxis: {
            categories: [
                '新创建',
                '进行中',
                '已完成',
                '已完成'
            ]
        },
        yAxis: {
            min: 0,
            title: {
                text: ''
            }
        },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.0f}</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        series: [
            {
                name: '任务数量',
                data: [2, 8, 6, 1],
                dataLabels: {
                    enabled: true,
                    rotation: 0,
                    color: '#FFFFFF',
                    align: 'right',
                    x: 4,
                    y: 10,
                    style: {
                        fontSize: '13px',
                        fontFamily: 'Verdana, sans-serif',
                        textShadow: '0 0 3px black'
                    }
                }
            }
        ]
    });

    $('#task-priority').highcharts({
        credits: {
            enabled: false
        },
        chart: {
            type: 'column'
        },
        title: {
            text: '任务优先级统计'
        },
        subtitle: {
            text: 'PowerTeam'
        },
        xAxis: {
            categories: [
                '低',
                '中',
                '高',
                '最高'
            ]
        },
        yAxis: {
            min: 0,
            title: {
                text: ''
            }
        },
        tooltip: {
            headerFormat: '<span style="font-size:10px">{point.key}</span><table>',
            pointFormat: '<tr><td style="color:{series.color};padding:0">{series.name}: </td>' +
                '<td style="padding:0"><b>{point.y:.0f}</b></td></tr>',
            footerFormat: '</table>',
            shared: true,
            useHTML: true
        },
        plotOptions: {
            column: {
                pointPadding: 0.2,
                borderWidth: 0
            }
        },
        series: [
            {
                name: '任务数量',
                data: [20, 12, 3, 2],
                dataLabels: {
                    enabled: true,
                    rotation: 0,
                    color: '#FFFFFF',
                    align: 'right',
                    x: 4,
                    y: 10,
                    style: {
                        fontSize: '13px',
                        fontFamily: 'Verdana, sans-serif',
                        textShadow: '0 0 3px black'
                    }
                }
            }
        ]
    });
};

function TeamCtrl($scope, $http) {
    $scope.AddSuccess = false;
    $scope.RemoveSuccess = false;
    $scope.MemberCandidate = [];
    $scope.MemberList = [];
    $scope.GetMemberCandidate = function () {
        $http.post('/Team/Candidate', { 'ProjectId': $scope.ProjectId }).success(function (data) {
            $scope.MemberCandidate = data;
        });
    };
    $scope.GetMemberInProject = function () {
        $http.post('/Team/InProject', { 'ProjectId': $scope.ProjectId }).success(function (data) {
            $scope.MemberList = data;
        });
    };
    $scope.AddMember = function (email) {
        $scope.AddSuccess = false;
        $scope.RemoveSuccess = false;
        $http.post('/Team/AddMember', { 'ProjectId': $scope.ProjectId, 'Email': email }).success(function (result) {
            if (result.Result == "OK") {
                $scope.AddSuccess = true;
                $scope.GetMemberCandidate();
                $scope.GetMemberInProject();
            }
        });
    };
    $scope.RemoveMember = function (userId) {
        $scope.AddSuccess = false;
        $scope.RemoveSuccess = false;
        $http.post('/Team/RemoveMember', { 'ProjectId': $scope.ProjectId, 'UserId': userId }).success(function (result) {
            if (result.Result == "OK") {
                $scope.RemoveSuccess = true;
                $scope.GetMemberCandidate();
                $scope.GetMemberInProject();
            }
        });
    };
};

function TaskListCtrl($scope, $http, $interval) {
    $scope.TaskList = [];
    $scope.Tags = [];
    $scope.MemberList = [];
    $scope.NewTask = {TaskName: null, Priority: 2, Status: 1, AssignTo: 0, AssignUser: "指派给", DeadLine: new Date() };
    //var NewTaskDesc = UE.getEditor('editor');
    var editor = $('#editor');
    editor.summernote();
    $scope.NewTaskCreated = false;
    $scope.Search = {
        ProjectId: 1,
        Keyword: null,
        New: true, InProgress: true, Completed: false, Canceled: false,
        Low: true, Normal: true, High: true,
        AssignTo: null,
        AssignToUser: '全部人员',
        TagId: null,
        TagName: '全部标签',
        PageNo: 1, RowCount: 0, PageSize: 20
    };

    $scope.GetTags = function () {
        $http.post('/Task/GetTags').success(function (data) {
            $scope.Tags = data;
            $scope.NewTask.Tags = angular.copy(data);
            $scope.DefaultNewTask = angular.copy($scope.NewTask);
        });
    };

    $scope.GetMemberInProject = function () {
        $http.post('/Team/InProject', { 'ProjectId': $scope.NewTask.ProjectId }).success(function (data) {
            $scope.MemberList = data;
        });
    };

    $scope.CreateNewTask = function () {
        $scope.NewTaskCreated = false;
        $scope.NewTask.Description = editor.code();
        $http.post("/Task/Create", $scope.NewTask).success(function (data) {
            if (data.Result == "OK") {
                $scope.NewTaskCreated = true;
                editor.code("");
                angular.copy($scope.DefaultNewTask, $scope.NewTask);
                $scope.Query();
                $("#newTask").collapse('hide');
                $interval(function () {
                    $scope.NewTaskCreated = false;
                }, 2000);
            }
        });
    };

    $scope.Query = function () {
        $http.post('/Task/Query', $scope.Search).success(function (data) {
            $scope.TaskList = data.TaskList;
            $scope.EditTaskList = angular.copy(data.TaskList);
            $scope.Search.RowCount = data.RowCount;
        });
    };
};

function TaskDetailCtrl($scope, $http) {
    $scope.Tags = [];
    $scope.MemberList = [];
    $scope.TaskId = null;
    $scope.Task = {};
    var editor = $('#editor');
    editor.summernote();
    $scope.GetTask = function () {
        $http.post('/Task/Get', {TaskId: $scope.TaskId}).success(function (data) {
            $scope.Task = data.Task;
            $scope.GetTags();
            $scope.GetMemberInProject();
            editor.code($scope.Task.Description);
        });
    };
    $scope.GetTags = function () {
        $http.post('/Task/GetTags').success(function (data) {
            $scope.Tags = data;
            $scope.Task.Tags = angular.copy(data);
        });
    };
    $scope.GetMemberInProject = function () {
        $http.post('/Team/InProject', { 'ProjectId': $scope.Task.ProjectId }).success(function (data) {
            $scope.MemberList = data;
        });
    };
};